package com.victualler.business.customer.controller.impl;

import com.victualler.business.customer.controller.PreCustomerOrderController;
import com.victualler.business.customer.dto.PreCustomerInfoDto;
import com.victualler.business.customer.dto.dataview.MounthQuantityDto;
import com.victualler.business.customer.dto.print.PreCustomerPrintDto;
import com.victualler.business.customer.dto.request.PreCustomerRequestDto;
import com.victualler.business.customer.dto.request.weixin.WxOrderRequestDto;
import com.victualler.business.customer.entity.PreCustomerOrderDao;
import com.victualler.business.customer.repository.PreCustomerOrderQueryRepository;
import com.victualler.business.customer.repository.PreCustomerOrderRepository;
import com.victualler.business.customer.service.PreCustomerOrderService;
import com.victualler.core.base.controller.impl.BaseRestControllerImpl;
import com.victualler.core.common.dto.JasperReportVO;
import com.victualler.core.common.dto.PageDto;
import com.victualler.core.common.enums.JasperTypeEnum;
import com.victualler.core.common.utils.ExceptionWapperUtil;
import com.victualler.core.common.utils.ReportUtil;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * @author: Guoshichang
 * @discription:    
 */
@RestController
@RequestMapping("/business/customer/pre/order")
public class PreCustomerOrderControllerImpl extends BaseRestControllerImpl<PreCustomerOrderDao,PreCustomerOrderRepository,PreCustomerOrderQueryRepository,PreCustomerOrderService> implements PreCustomerOrderController {

    /**
     * @author Guoshichang
     * @since 13:39 on 2018/7/7.
     * @param preCustomerRequestDto 请求对象
     * @Function  根据条件入参prePurchaseRequestDto动态分页查询权限
     */
    @RequestMapping("/getByRequestDto")
    public PageDto<PreCustomerOrderDao> getByRequestDto(@RequestBody PreCustomerRequestDto preCustomerRequestDto, @RequestParam("pageNo") int pageNo, @RequestParam("limit") int limit) {
        return this.baseService.getByRequestDto(preCustomerRequestDto,pageNo,limit);
    }

    /**
     * @author Guoshichang
     * @since 13:39 on 2018/7/7.
     * @param id 预订单id
     * @Function  根据条件入参prePurchaseRequestDto动态分页查询权限
     */
    @RequestMapping("/findInfo/{id}")
    public PreCustomerInfoDto findInfo(@PathVariable("id") Long id) {
        return this.baseService.findInfo(id);
    }

    /**
     * @author Guoshichang
     * @since 13:39 on 2018/7/7.
     * @param preCustomerInfoDto 预订单实体大对象
     * @Function  保存预订单实体大对象
     */
    @RequestMapping("/saveInfo")
    public void saveInfo(@RequestBody PreCustomerInfoDto preCustomerInfoDto) {
        this.baseService.saveInfo(preCustomerInfoDto);
    }

    /**
     * @author Guoshichang
     * @since 18:01 on 2018/10/9.
     * @param preCustomerOrderDao  大对象
     * @return
     * @Function
     */
    @RequestMapping("/check")
    public void check(@RequestBody PreCustomerOrderDao preCustomerOrderDao) {
        this.baseService.check(preCustomerOrderDao);
    }

    /**
     * @author Guoshichang
     * @since 13:39 on 2018/7/7.
     * @param id 预订单id
     * @Function  根据条件入参prePurchaseRequestDto动态分页查询权限
     */
    @RequestMapping("/deleteInfo/{id}")
    public void deleteInfo(@PathVariable("id") Long id) {
        this.baseService.deleteInfo(id);
    }



    /**
     * @author Guoshichang
     * @since 19:54 on 2018/7/23.
     * @param response 存放文件的 输出流
     * @param orderNo 订单号
     * @Function 将文件从影像系统下载下来并且打印返回到response的流中
     */
    @RequestMapping("/exportFile/{type}/{orderNo}")
    public void exportFile(@PathVariable("type")String type,@PathVariable("orderNo") String orderNo, HttpServletResponse response) {
        //输出
        try {
            if(type.equals("orderString")){
                //将订单输出为条形码标识的文本文件 用于对接第三方系统
                String result = this.baseService.getExportModelText(orderNo);
                response.setContentType("txt text/plain");
                response.addHeader("Content-Disposition", "attachment; FileName="+orderNo+".txt");
                response.getOutputStream().write(result.getBytes());
            } else {
                //从service中获取对应的对象
                List<PreCustomerPrintDto> modelList = this.baseService.getExportModelList(orderNo);
                //输出到文件流
                response.setContentType(JasperTypeEnum.PDF.getContentType());
                String  jasperModelName ="/jasper/PreCustomerPrint.jasper";
                if(type.equals("bigger")){
                    jasperModelName ="/jasper/PreCustomerPrintA4.jasper";
                }
                ReportUtil.reportData(Arrays.asList(new JasperReportVO(jasperModelName,modelList)),null,JasperTypeEnum.PDF,response.getOutputStream());
            }
        } catch (IOException e) {
            throw ExceptionWapperUtil.wapper(e);
        }
    }

    /**
     * @author Guoshichang
     * @since 12:00 on 2019/1/18.
     * @param wxOrderRequestDto 微信购物车下单大对象
     * @return 无
     * @Function  将微信报货单报货对象
     */
    @RequestMapping("/sendWeiXinOrder")
    public void sendWeiXinOrder(@RequestBody WxOrderRequestDto wxOrderRequestDto) {
        this.baseService.sendWeiXinOrder(wxOrderRequestDto);
    }

    @RequestMapping("/getSumQuantityDataView")
    public List<MounthQuantityDto> getSumQuantityDataView() {
        return this.baseService.getSumQuantityDataView();
    }

    @RequestMapping("/getQuantityByGoods")
    public List<MounthQuantityDto> getQuantityByGoods() {
        return this.baseService.getQuantityByGoods();
    }

    /**
     * @author Guoshichang
     * @since 11:52 on 2019/1/31.
     * @param
     * @return
     * @Function 获得前三十天内销量top10的商家
     */
    @RequestMapping("/getQuantityByCustomer")
    public List<MounthQuantityDto> getQuantityByCustomer() {
        return this.baseService.getQuantityByCustomer();
    }
}
